<?php

require_once '../util.php';
require_once '../config.php';

connect_db_mute();

function add_retweet_task($tweet_id, $emails, $all_retweet_content) {
  $tweet_id = mysql_real_escape_string($tweet_id);
  $count = 0;
  $last_content = end($all_retweet_content);
  $account_key = 0;
  foreach($emails as $email) {
    $email = mysql_real_escape_string($email);
    $retweet_content = $all_retweet_content[$account_key];
    $content = "$tweet_id#$retweet_content";
    $sql = sprintf("insert into task(email, type, content) values ('%s', 'retweet', '%s')",
                   $email, $content);
    $ret = sql_or_die($sql);
    $count += mysql_affected_rows();
    if ($retweet_content == $last_content)
        $account_key = 0;
    else
        $account_key = $account_key + 1;
  }
  return $count;
}

$emails = $_POST["emails"];
$tweet_id = $_POST["tweet_id"];
$all_retweet_content = $_POST["retweet_content"];

$count = add_retweet_task($tweet_id, $emails, $all_retweet_content);
echo "successfully updated $count rows";

?>
